﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Text;
using System.Windows.Forms;
using QD.Snapshot.Core.BLL;
using QD.Snapshot.Core.Form;
using QD.Snapshot.Core.Config;
using QD.Utill;

namespace QD.Snapshot
{
    public partial class frmDefault : FormBase
    {
        List<Thread> listThread = new List<Thread>();
        public frmDefault()
        {
            InitializeComponent();
        }

        private void frmDefault_Load(object sender, EventArgs e)
        {
            SiteConfig.Init();
            UrlDomain.Init();
            Begin();
        }

        void Begin()
        {
            #region 2017-09-11 wanghui add 异常捕获
            try
            {
                //设置应用程序处理异常方式：ThreadException处理 
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //处理UI线程异常 
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                //处理非UI线程异常 
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            }
            catch (Exception ex)
            {
                string str = GetExceptionMsg(ex, string.Empty);
                Log("error", str);
            }
            #endregion

            if (SiteConfig.Engine.Contains("百度"))
                listThread.Add(Work.Begin_BD(txtMsg_BD));

            if (SiteConfig.Engine.Contains("360"))
                listThread.Add(Work.Begin_360(txtMsg_360));

            if (SiteConfig.Engine.Contains("搜狗"))
                listThread.Add(Work.Begin_SG(txtMsg_SG));

            if (SiteConfig.Engine.Contains("微信"))
                listThread.Add(Work.Begin_WX(txtMsg_Weixin));

            if (SiteConfig.Engine.Contains("bing"))
                listThread.Add(Work.Begin_Bing(txtMsg_Bing));

            if (SiteConfig.Engine.Contains("中国搜索"))
                listThread.Add(Work.Begin_GuoSou(txtMsg_GuoSou));

            timer1.Start();
        }

        private void frmDefault_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();
            AbortThread(listThread);
        }

        static DateTime timeDomain = DateTime.Now;
        private void timer1_Tick(object sender, EventArgs e)
        {
            lblNum_bd.Text = string.Format("{0}个词/小时", (NumHelper.Instance.NumBD * 3 * 60));
            lblNum_360.Text = string.Format("{0}个词/小时", (NumHelper.Instance.Num360 * 3 * 60));
            lblNum_sogou.Text = string.Format("{0}个词/小时", (NumHelper.Instance.NumSogou * 3 * 60));
            lblNum_WX.Text = string.Format("{0}个词/小时", (NumHelper.Instance.NumWeixin * 3 * 60));
            lblNum_Bing.Text = string.Format("{0}个词/小时", (NumHelper.Instance.NumBing * 3 * 60));
            lblNum_GuoSou.Text = string.Format("{0}个词/小时", (NumHelper.Instance.NumGuoSou * 3 * 60));

            Log("time", string.Format("百度:{0}\t360:{1}\t搜狗:{2}\tBing:{3}\t国搜:{4}\t微信:{5}", lblNum_bd.Text, lblNum_360.Text, lblNum_sogou.Text, lblNum_Bing.Text, lblNum_GuoSou.Text,lblNum_WX.Text));

            //24小时更新平台网址
            if (DateTime.Now.Subtract(timeDomain).TotalHours >= 24)
            {
                UrlDomain.Init();
            }
        }
    }
}